home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2624 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  74 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: storing address of member functions, HELP!
  5. Date: 18 Jan 1996 19:57:33 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dm8nd$q2d@colossus.holonet.net>
  8. References: <4djbj4$5nu@news-rocq.inria.fr> <4dju3b$ehi@gateway.comsearch.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Thuan Nguyen (thnguyen@comsearch.com) wrote:
  13. : Sophie.Cluet@inria.fr (Sophie Cluet) wrote:
  14.  
  15. [some deletions in the following]
  16.  
  17. : >  Class nary_op 
  18. : >    {...
  19. : >     int evaluate();    // evaluate the parameters and invoke
  20. : >                        // the below stored function (code)
  21. : >     int (nary_op::*code)();  // address of the evaluation function
  22. : >     int build_list(); // one such evaluation fonction 
  23. : >     ...}
  24. : >  Class struct_op: public nary_op
  25. : >    {char ** names;  // added stored information
  26. : >     int build_struct();  // an evaluation function 
  27. : >                          // that uses the above names
  28. : >     ...}
  29. : >Now, the problem. The compiler accepts neither of the following 
  30. : >instructions on a nary_op:
  31. : >    
  32. : >     code=&struct_op::build_struct;
  33. : >  or
  34. : >     code=(int (nary_op::*)())&struct_op::build_struct;
  35. : >
  36. [...]
  37. : Let's me try. The scope of the function is the same as the scope of the object
  38. : itself. In code = &struct_op::build_struct; the object doesnt exist.
  39. : How about declaring the build_struct function static.
  40.  
  41. No.  &struct_op::build_struct does not need an object, so your
  42. suggestion is based on an incorrect assumption.  Static members
  43. would change the picture, indeed, but I don't think they're what
  44. we want here. 
  45.  
  46. There simply is NO WAY to store a struct_op::* in a nary_op::*
  47. variable.  Casting is not allowed for this kind of pointer.
  48.  
  49. Anyway, if you think about it, you really wouldn't want evaluate(),
  50. a base-class member, to access build_struct(), a derived-class
  51. member, even via pointer.  The only time downward access like that
  52. makes OO sense (or is possible in C++) is if the function is virtual.
  53.  
  54. If your design is otherwise sound, I think virtual functions
  55. may be what you're looking for here.  Perhaps build_struct 
  56. performs the same function (but differently) as some base-class
  57. member which you already have; if so, declare that member virtual
  58. and rename build_struct to override this function.  Or else,
  59. just declare a dummy virtual int build_struct() in your base
  60. class.  (The latter is not as good from an OO standpoint.)  Then
  61.      code = &nary_op::build_struct;   // does what you want!
  62.  
  63. Here's a code snippet that illustrates my point:
  64.  
  65. class B { public:    virtual void foo() { cout << "base\n"; } };
  66. class D : public B { public: void foo() { cout << "der\n"; } };
  67. int main() {
  68.    D d;
  69.    void (B::*ptr)() = &B::foo;
  70.    (d.*ptr)();                   // prints "der"
  71. };
  72. --
  73. Russell Blackadar,   russell@mdli.com
  74.